home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / testylesample / testylesample.p < prev    next >
Encoding:
Text File  |  2000-09-28  |  52.2 KB  |  1,737 lines

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    MultiFinder-Aware Simple Styled TextEdit Sample Application
  6. #
  7. #    TEStyleSample
  8. #
  9. #    TEStyleSample.p    -    Pascal Source
  10. #
  11. #    Copyright © 1989 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                        10/89
  15. #
  16. #    Components:    TEStyleSample.p            October    1, 1989
  17. #                TEStyleSampleGlue.a        October    1, 1989
  18. #                TEStyleSample.r            October    1, 1989
  19. #                TEStyleSample.h            October    1, 1989
  20. #                PTEStyleSample.make        October    1, 1989
  21. #
  22. #    TEStyleSample is an example application that demonstrates how 
  23. #    to initialize the commonly used toolbox managers, operate 
  24. #    successfully under MultiFinder, handle desk accessories and 
  25. #    create, grow, and zoom windows. Both styled and fundamental TextEdit 
  26. #    toolbox calls and TextEdit autoscroll are demonstrated. It 
  27. #    also shows how to create and maintain scrollbar controls as well
  28. #    as implementing a basic printing loop.
  29. #
  30. #    It does not by any means demonstrate all the techniques you 
  31. #    need for a large application. In particular, TEStyleSample does not 
  32. #    cover exception handling, multiple windows/documents, 
  33. #    sophisticated memory management, or undo. All of 
  34. #    these are vital parts of a normal full-sized application.
  35. #
  36. #    This application is an example of the form of a Macintosh 
  37. #    application; it is NOT a template. It is NOT intended to be 
  38. #    used as a foundation for the next world-class, best-selling, 
  39. #    600K application. A stick figure drawing of the human body may 
  40. #    be a good example of the form for a painting, but that does not 
  41. #    mean it should be used as the basis for the next Mona Lisa.
  42. #
  43. #    We recommend that you review this program, TESample or Sample before 
  44. #    beginning a new application. TESample is a simpler version of TEStyleSample
  45. #    without styles and Sample is a simple app. which doesn’t 
  46. #    use TextEdit or the Control Manager.
  47. #
  48. ------------------------------------------------------------------------------}
  49.  
  50.  
  51. PROGRAM TEStyleSample;
  52.  
  53. {Segmentation strategy:
  54.  
  55.  This program consists of three segments. Main contains most of the code,
  56.  including the MPW libraries, and the main program. Initialize contains
  57.  code that is only used once, during startup, and can be unloaded after the
  58.  program starts. %A5Init is automatically created by the Linker to initialize
  59.  globals for the MPW libraries and is unloaded right away.}
  60.  
  61.  
  62. {SetPort strategy:
  63.  
  64.  Toolbox routines do not change the current port. In spite of this, in this
  65.  program we use a strategy of calling SetPort whenever we want to draw or
  66.  make calls which depend on the current port. This makes us less vulnerable
  67.  to bugs in other software which might alter the current port (such as the
  68.  bug (feature?) in many desk accessories which change the port on OpenDeskAcc).
  69.  Hopefully, this also makes the routines from this program more self-contained,
  70.  since they don't depend on the current port setting.}
  71.  
  72.  
  73. {Clipboard strategy: 
  74.  
  75.  Under styled TextEdit, TECut and TECopy will write both the text and associated
  76.  style information directly to the desk scrap as types 'TEXT' and 'styl'.
  77.  Instead of using TEToScrap and TEFromScrap, a new routine TEStylePaste, will 
  78.  transfer the text and style from the desk scrap to the document. }
  79.  
  80. {$D+}
  81.  
  82. USES
  83.     Types, QuickDraw, Events, Controls, Windows, TextEdit, Dialogs, Fonts, Lists,
  84.     Menus, Resources, Scrap, ToolUtils, 
  85.     OSUtils, Files, Devices, DeskBus, DiskInit, Disks, Errors, Memory, Retrace, SegLoad, Serial,
  86.     ShutDown, Slots, Sound, Start, Timer, Packages, Traps, Printing;
  87.  
  88. CONST
  89.  
  90.     {kTextMargin is the number of pixels we leave blank at the edge of the window.}
  91.     kTextMargin                = 2;
  92.  
  93.     {kMaxOpenDocuments is used to determine whether a new document can be opened
  94.      or created. We keep track of the number of open documents, and disable the
  95.      menu items that create a new document when the maximum is reached. If the
  96.      number of documents falls below the maximum, the items are enabled again.}
  97.     kMaxOpenDocuments        = 1;
  98.     
  99.     {kMaxDocWidth is an arbitrary number used to specify the width of the TERec's
  100.     destination rectangle so that word wrap and horizontal scrolling can be
  101.     demonstrated.}
  102.     kMaxDocWidth            = 576;
  103.     
  104.     {kMinDocDim is used to limit the minimum dimension of a window when GrowWindow
  105.     is called.}
  106.     kMinDocDim                = 64;
  107.     
  108.     {kControlInvisible is used to 'turn off' controls (i.e., cause the control not
  109.     to be redrawn as a result of some Control Manager call such as SetControlValue)
  110.     by being put into the contrlVis field of the record. kControlVisible is used
  111.     the same way to 'turn on' the control.}
  112.     kControlInvisible        = 0;
  113.     kControlVisible            = $FF;
  114.  
  115.     {kScrollBarAdjust and kScrollBarWidth are used in calculating
  116.     values for control positioning and sizing.}
  117.     kScrollbarWidth            = 16;
  118.     kScrollbarAdjust        = kScrollbarWidth - 1;
  119.  
  120.     {kScrollTweek compensates for off-by-one requirements of the scrollbars
  121.      to have borders coincide with the growbox.}
  122.     kScrollTweek            = 2;
  123.  
  124.     {kCrChar is used to match with a carriage return when calculating the
  125.     number of lines in the TextEdit record. kDelChar is used to check for
  126.     delete in keyDowns.}
  127.     kCRChar                    = 13;
  128.     kDelChar                = 8;
  129.  
  130.     {kButtonScroll is how many pixels to scroll horizontally when the button part
  131.     of the horizontal scrollbar is pressed.}
  132.     kButtonScroll            = 4;
  133.     
  134.     {kMaxTELength is an arbitrary number used to limit the length of text in the TERec
  135.     so that various errors won't occur from too many characters in the text.}
  136.     kMaxTELength            = 32000;
  137.     (* what about that tech note I wrote? is this a valid check anymore? maw *)
  138.  
  139.     {kSysEnvironsVersion is passed to SysEnvirons to tell it which version of the
  140.      SysEnvRec we understand.}
  141.     kSysEnvironsVersion        = 1;
  142.  
  143.     {kOSEvent is the event number of the suspend/resume and mouse-moved events sent
  144.      by MultiFinder. Once we determine that an event is an osEvent, we look at the
  145.      high byte of the message sent to determine which kind it is. To differentiate
  146.      suspend and resume events we check the resumeMask bit.}
  147.     kOSEvent                = app4Evt;    { event used by MultiFinder }
  148.     kSuspendResumeMessage    = 1;        { high byte of suspend/resume event message }
  149.     kResumeMask                = 1;        { bit of message field for resume vs. suspend }
  150.     kMouseMovedMessage        = $FA;        { high byte of mouse-moved event message }
  151.     kNoEvents                = 0;        {no events mask}
  152.  
  153.     {kMinHeap - This is the minimum result from the following
  154.      equation:
  155.             
  156.             ORD(GetApplLimit) - ORD(ApplicationZone)
  157.             
  158.      for the application to run. It will insure that enough memory will
  159.      be around for reasonable-sized scraps, FKEYs, etc. to exist with the
  160.      application, and still give the application some 'breathing room'.
  161.      To derive this number, we ran under a MultiFinder partition that was
  162.      our requested minimum size, as given in the 'SIZE' resource.}
  163.      
  164.     kMinHeap    = 29 * 1024; 
  165.  
  166.     {kMinSpace - This is the minimum result from PurgeSpace, when called
  167.      at initialization time, for the application to run. This number acts
  168.      as a double-check to insure that there really is enough memory for the
  169.      application to run, including what has been taken up already by
  170.      pre-loaded resources, the scrap, code, and other sundry memory blocks.}
  171.      
  172.     kMinSpace    = 20 * 1024;
  173.     
  174.     {kExtremeNeg and kExtremePos are used to set up wide open rectangles and regions.}
  175.     kExtremeNeg        = -32768;
  176.     kExtremePos        = 32767 - 1;    { required for old region bug }
  177.     
  178.     {kTESlop provides some extra security when pre-flighting edit commands.}
  179.     kTESlop            = 1024;
  180.  
  181.     {kErrStrings is the resource ID for the error strings STR# resource.}
  182.     kErrStrings        = 128;
  183.  
  184.     { The following are indicies into STR# resources. }
  185.     eWrongMachine    = 1;
  186.     eSmallSize        = 2;
  187.     eNoMemory        = 3;
  188.     eNoSpaceCut        = 4;
  189.     eNoCut            = 5;
  190.     eNoCopy            = 6;
  191.     eExceedPaste    = 7;
  192.     eNoSpacePaste    = 8;
  193.     eNoWindow        = 9;
  194.     eExceedChar        = 10;
  195.     eNoPaste        = 11;
  196.     
  197.     
  198.     { The following constants are all resource IDs, corresponding to their resources }
  199.     
  200.     rMenuBar    = 128;                { application's menu bar }
  201.     rAboutAlert    = 128;                { about alert }
  202.     rUserAlert    = 129;                { user error alert }
  203.     rDocWindow    = 128;                { application's window }
  204.     
  205.     rVScroll    = 128;                { vertical scrollbar control }
  206.     rHScroll    = 129;                { horizontal scrollbar control }
  207.  
  208.  
  209.     { The following constants are all menu and item IDs corresponding to their resources }
  210.     
  211.     mApple        = 128;                { Apple menu }
  212.     iAbout        = 1;
  213.  
  214.     mFile        = 129;                { File menu }
  215.     iNew        = 1;
  216.     iClose        = 4;
  217.     iPageSetup    = 9;                { Added for TEStyleSample }
  218.     iPrint        = 10;                { Added for TEStyleSample }
  219.     iQuit        = 12;
  220.  
  221.     mEdit        = 130;                { Edit menu }
  222.     iUndo        = 1;
  223.     iCut        = 3;
  224.     iCopy        = 4;
  225.     iPaste        = 5;
  226.     iClear        = 6;
  227.     iSelectAll    = 8;                { Added for TEStyleSample }
  228.     
  229.     mFont         = 131;                { Font menu-added for TEStyleSample }
  230.     
  231.     mFontSize     = 132;                { Font size menu-added for TEStyleSample }
  232.     iNine        = 1;
  233.     iTen        = 2;
  234.     iTwelve        = 3;
  235.     iFourteen    = 4;
  236.     iEighteen    = 5;
  237.     iTwoFour    = 6;
  238.     
  239.     mStyle         = 133;                { Style menu-added for TEStyleSample }
  240.     iPlain         = 1;
  241.     iBold         = 3;
  242.     iItalic        = 4;
  243.     iUnderline     = 5;
  244.     iOutline     = 6;
  245.     iShadow     = 7; 
  246.     
  247.     {kDITop and kDILeft are used to locate the Disk Initialization dialogs.}
  248.     kDITop        = $0050;
  249.     kDILeft        = $0070;
  250.  
  251.     
  252. TYPE
  253.     {A DocumentRecord contains the WindowRecord for one of our document windows,
  254.      as well as the TEHandle for the text we are editing. We have added fields to
  255.      hold the ControlHandles to the vertical and horizontal scrollbars and to hold
  256.      the address of the default clickLoop that gets attached to a TERec when you call
  257.      TEAutoView. Other document fields can be added to this record as needed. For
  258.      a similar example, see how the Window Manager and Dialog Manager add fields
  259.      after the GrafPort.}
  260.     DocumentRecord        = RECORD
  261.         docWindow        : WindowRecord;
  262.         docTE            : TEHandle;
  263.         docVScroll        : ControlHandle;
  264.         docHScroll        : ControlHandle;
  265.         docClick        : TEClickLoopUPP;
  266.     END;
  267.     DocumentPeek        = ^DocumentRecord;
  268.  
  269.  
  270. VAR
  271.     {The "g" prefix is used to emphasize that a variable is global.}
  272.     gMac                : SysEnvRec;    { set up by Initialize }
  273.     gHasWaitNextEvent    : BOOLEAN;        { set up by Initialize }
  274.     gInBackground        : BOOLEAN;        { maintained by Initialize and DoEvent }
  275.     gNumDocuments        : INTEGER;        { maintained by Initialize, DoNew, and DoCloseWindow }
  276.     
  277.     {New globals to support printing and style selection }
  278.     gTxStyle             : TextStyle;    { holds style selected, plain default, maintained by DoMenuCommand }
  279.     gFontName             : Str255;        { name of font selected, app font default, maintained by DoMenuCommand }
  280.     gFontID             : INTEGER;        { ID of font selected, app font default, maintained by DoMenuCommand }
  281.     gFontSize             : LONGINT;        { font size selected, 12 pt default, maintained by DoMenuCommand }
  282.     gPrinterRecord        : THPrint;        { print handle, maintained by printText }
  283.     gPrinterPort        : TPPrPort;        { pointer to Print Manager's GrafPort }
  284.  
  285.  
  286.  
  287. {$S Initialize}
  288. FUNCTION TrapAvailable(tNumber: INTEGER; tType: TrapType): BOOLEAN;
  289.  
  290. {Check to see if a given trap is implemented. This is only used by the
  291.  Initialize routine in this program, so we put it in the Initialize segment.
  292.  The recommended approach to see if a trap is implemented is to see if
  293.  the address of the trap routine is the same as the address of the
  294.  Unimplemented trap.}
  295. {Needs to be called after call to SysEnvirons so that it can check
  296.  if a ToolTrap is out of range of a pre-MacII ROM.}
  297.  
  298. BEGIN
  299.     IF (tType = ToolTrap) &
  300.         (gMac.machineType > envMachUnknown) &
  301.         (gMac.machineType < envMacII) THEN BEGIN        {it's a 512KE, Plus, or SE}
  302.         tNumber := BAND(tNumber, $03FF);
  303.         IF tNumber > $01FF THEN                            {which means the tool traps}
  304.             tNumber := _Unimplemented;                    {only go to $01FF}
  305.     END;
  306.     TrapAvailable := NGetTrapAddress(tNumber, tType) <>
  307.                         NGetTrapAddress(_Unimplemented, ToolTrap);
  308. END; {TrapAvailable}
  309.  
  310.  
  311. {$S Main}
  312. FUNCTION IsDAWindow( window : WindowPtr ) : BOOLEAN;
  313.  
  314. {  Check if a window belongs to a desk accessory. }
  315.  
  316. BEGIN { IsDAWindow }
  317.     IF window = NIL THEN
  318.         IsDAWindow := FALSE
  319.     ELSE    { DA windows have negative windowKinds }
  320.         IsDAWindow := WindowPeek( window )^.windowKind < 0;
  321. END; { IsDAWindow }
  322.  
  323.  
  324.  
  325. {$S Main}
  326. FUNCTION IsAppWindow( window : WindowPtr ) : BOOLEAN;
  327.  
  328. {  Check if a window belongs to the application. }
  329.  
  330. BEGIN { IsAppWindow }
  331.     IF window = NIL THEN
  332.         IsAppWindow := FALSE
  333.     ELSE    { application windows have non-negative windowKinds }
  334.         IsAppWindow := WindowPeek( window )^.windowKind >= 0;
  335. END; { IsAppWindow }
  336.  
  337.  
  338.  
  339. {$S Main}
  340. PROCEDURE AlertUser( error : INTEGER );
  341.  
  342. {  Display an alert that tells the user an error occurred, then exit the program }
  343.  
  344. VAR
  345.     itemHit    : INTEGER;
  346.     message    : Str255;
  347.  
  348. BEGIN { AlertUser }
  349.     SetCursor( qd.arrow );
  350.     GetIndString( message, kErrStrings, error );
  351.     ParamText(message, '', '', '');
  352.     itemHit := Alert( rUserAlert, NIL );
  353. END; { AlertUser }
  354.  
  355.  
  356.  
  357. {$S Main}
  358. PROCEDURE GetTERect( window : WindowPtr; VAR teRect : Rect);
  359.  
  360. {   return a rectangle that is inset from the portRect by the size of
  361.     the scrollbars and a little extra margin. }
  362.  
  363. BEGIN { GetTERect }
  364.     teRect := window^.portRect;
  365.     InsetRect( teRect, kTextMargin, kTextMargin );        { adjust for margin }
  366.     teRect.bottom := teRect.bottom - kScrollbarAdjust;    { and for the scrollbars }
  367.     teRect.right := teRect.right - kScrollbarAdjust;
  368. END; { GetTERect }
  369.  
  370.  
  371.  
  372. {$S Main}
  373. FUNCTION DoCloseWindow( window : WindowPtr ) : BOOLEAN;
  374.  
  375. {    Close a window. This handles desk accessory and application windows. }
  376.  
  377. BEGIN { DoCloseWindow }
  378.     DoCloseWindow := TRUE;
  379.     IF IsDAWindow( window ) THEN
  380.         CloseDeskAcc( WindowPeek( window )^.windowKind )
  381.     ELSE IF IsAppWindow( window ) THEN BEGIN
  382.         WITH DocumentPeek( window )^ DO
  383.             IF docTE <> NIL THEN
  384.                 TEDispose( docTE );
  385.         CloseWindow( window );
  386.         DisposePtr( Ptr( window ) );
  387.         gNumDocuments := gNumDocuments - 1;
  388.     END;
  389. END; { DoCloseWindow }
  390.  
  391.  
  392.  
  393. {$S Main}
  394. PROCEDURE AdjustTE( window : WindowPtr );
  395.  
  396. {    Scroll the TERec around to match up to the potentially updated scrollbar
  397.     values. This is really useful when the window resizes such that the
  398.     scrollbars become inactive and the TERec had been previously scrolled. }
  399.  
  400. VAR
  401.     value    : INTEGER;
  402.     
  403. BEGIN { AdjustTE }
  404.     WITH DocumentPeek( window )^ DO BEGIN
  405.         TEScroll( ( docTE^^.viewRect.left - docTE^^.destRect.left ) - GetControlValue( docHScroll ),
  406.                 ( docTE^^.viewRect.top - docTE^^.destRect.top ) -
  407.                 GetControlValue( docVScroll ) , docTE );
  408.     END; { with }
  409. END; { AdjustTE }
  410.  
  411.  
  412.  
  413. {$S Main}
  414. PROCEDURE AdjustHV( isVert : BOOLEAN; control : ControlHandle; 
  415.                     docTE : TEHandle; canRedraw : BOOLEAN );
  416.                     
  417. {Calculate the new control maximum value and current value, whether it is the horizontal or
  418. vertical scrollbar. The vertical max is calculated by comparing the number of lines to the
  419. vertical size of the viewRect. The horizontal max is calculated by comparing the maximum document
  420. width to the width of the viewRect. The current values are set by comparing the offset between
  421. the view and destination rects. If necessary and we canRedraw, have the control be re-drawn by
  422. calling ShowControl.}
  423.  
  424. {TEStyleSample-vertical max originally used line by line calculations-lineheight was a
  425. constant value so it was easy to figure out what the range should be and pin the value
  426. within range. Now we need to use max and min values in pixels rather than in nlines}
  427.  
  428. VAR
  429.     value, max             : INTEGER;
  430.     oldValue, oldMax    : INTEGER;
  431.     
  432. BEGIN { AdjustHV }
  433.     oldValue := GetControlValue( control );
  434.     oldMax := GetControlMaximum( control );
  435.     IF isVert THEN BEGIN
  436.         { new for TEStyleSample }
  437.         max := ( TEGetHeight( docTE^^.nLines, 0, docTE ) ) - 
  438.                             ( docTE^^.viewRect.bottom - docTE^^.viewRect.top );
  439.         
  440.     END ELSE
  441.         max := kMaxDocWidth - (docTE^^.viewRect.right - docTE^^.viewRect.left );
  442.     
  443.     IF max < 0 THEN
  444.         max := 0;            { check for negative values }
  445.     SetControlMaximum( control, max );
  446.     IF isVert THEN
  447.         value := docTE^^.viewRect.top - docTE^^.destRect.top 
  448.     ELSE
  449.         value := docTE^^.viewRect.left - docTE^^.destRect.left;
  450.     IF value < 0 THEN
  451.         value := 0
  452.     ELSE IF value > max THEN
  453.         value := max;        { pin the value to within range }
  454.     SetControlValue( control, value );
  455.     IF canRedraw & ( ( max <> oldMax ) | ( value <> oldValue ) ) THEN
  456.         ShowControl( control );            { check to see if the control can be re-drawn }
  457. END; { AdjustHV }
  458.  
  459.  
  460.  
  461. {$S Main}
  462. PROCEDURE AdjustScrollValues( window : WindowPtr; canRedraw : BOOLEAN );
  463.  
  464. {    Simply call the common adjust routine for the vertical and horizontal scrollbars. }
  465.  
  466. BEGIN { AdjustScrollValues }
  467.     WITH DocumentPeek( window )^ DO BEGIN
  468.         AdjustHV( TRUE, docVScroll, docTE, canRedraw );
  469.         AdjustHV( FALSE, docHScroll, docTE, canRedraw );
  470.     END; { with }
  471. END; { AdjustScrollValues }
  472.  
  473.  
  474.  
  475. {$S Main}
  476. PROCEDURE AdjustScrollSizes( window : WindowPtr );
  477.  
  478. {    Re-calculate the position and size of the viewRect and the scrollbars.
  479.      kScrollTweek compensates for off-by-one requirements of the scrollbars
  480.     to have borders coincide with the growbox. }
  481.  
  482. VAR
  483.     teRect    : Rect;
  484.  
  485. BEGIN { AdjustScrollSizes }
  486.     GetTERect( window, teRect ); {start with teRect}
  487.     WITH DocumentPeek( window )^, window^.portRect DO BEGIN
  488.         docTE^^.viewRect := teRect;
  489.         
  490.         { AdjustViewRect(docTE) was removed--no longer needed }
  491.         
  492.         MoveControl( docVScroll, right - kScrollbarAdjust, -1 );
  493.         SizeControl( docVScroll, kScrollbarWidth, ( bottom - top ) -
  494.                         ( kScrollbarAdjust - kScrollTweek ) );
  495.         MoveControl( docHScroll, -1, bottom - kScrollbarAdjust );
  496.         SizeControl( docHScroll, ( right - left ) - ( kScrollbarAdjust -
  497.                         kScrollTweek ), kScrollbarWidth );
  498.     END; { with }
  499. END; { AdjustScrollSizes }
  500.  
  501.  
  502.  
  503. {$S Main}
  504. PROCEDURE AdjustScrollbars( window : WindowPtr; needsResize : BOOLEAN );
  505.  
  506. {    Turn off the controls by jamming a zero into their contrlVis fields 
  507.     (HideControl erases them and we don't want that). If the controls are to 
  508.     be resized as well, call the procedure to do that, then call the procedure 
  509.     to adjust the maximum and current values. Finally re-enable the controls
  510.     by jamming a $FF in their contrlVis fields. }
  511.  
  512. VAR
  513.     oldMax, oldVal    : INTEGER;
  514.  
  515. BEGIN { AdjustScrollbars }
  516.     WITH DocumentPeek( window )^ DO BEGIN
  517.         docVScroll^^.contrlVis := kControlInvisible; { turn them off }
  518.         docHScroll^^.contrlVis := kControlInvisible;
  519.         IF needsResize THEN                            { move and size if needed }
  520.             AdjustScrollSizes( window );
  521.         AdjustScrollValues( window, NOT needsResize ); { fool with max and current value }
  522.         { Now, restore visibility in case we never had to ShowControl during adjustment }
  523.         docVScroll^^.contrlVis := kControlVisible; { turn them on }
  524.         docHScroll^^.contrlVis := kControlVisible;
  525.     END;
  526. END; { AdjustScrollbars }
  527.  
  528.  
  529.  
  530. {$S Main}
  531. {$PUSH} {$Z+}
  532. PROCEDURE PascalClickLoop;
  533.  
  534. {    Gets called from our assembly language routine, AsmClickLoop, which is in
  535.      turn called by the TEClick toolbox routine. Saves the windows clip region,
  536.      sets it to the portRect, adjusts the scrollbar values to match the TE scroll
  537.      amount, then restores the clip region. }
  538.  
  539. VAR
  540.     window : WindowPtr;
  541.     region : RgnHandle;
  542.  
  543. BEGIN { PascalClickLoop }
  544.     window := FrontWindow;
  545.     region := NewRgn;
  546.     GetClip( region ); { save the old clip }
  547.     ClipRect( window^.portRect ); { set the new clip }
  548.     AdjustScrollValues( window, TRUE ); { pass TRUE for canRedraw }
  549.     SetClip( region ); { restore the old clip }
  550.     DisposeRgn( region );
  551. END; { PascalClickLoop }
  552. {$POP}
  553.  
  554.  
  555.  
  556. {$S Main}
  557. {$PUSH} {$Z+}
  558. FUNCTION GetOldClickLoop : TEClickLoopUPP;
  559.  
  560. {    Gets called from our assembly language routine, AsmClickLoop, which is in
  561.     turn called by the TEClick toolbox routine. It returns the address of the
  562.     default clickLoop routine that was put into the TERec by TEAutoView to
  563.     AsmClickLoop so that it can call it. }
  564.  
  565. BEGIN { GetOldClickLoop }
  566.     GetOldClickLoop := DocumentPeek( FrontWindow )^.docClick;
  567. END; { GetOldClickLoop }
  568. {$POP}
  569.  
  570.  
  571.  
  572. PROCEDURE AsmClickLoop; EXTERNAL;
  573.  
  574. {    A reference to our assembly language routine that gets attached to the clickLoop
  575.     field of our TE record. }
  576.  
  577.  
  578.  
  579. {$S Main}
  580. PROCEDURE DoNew;
  581.  
  582. {    Create a new document and window. }
  583.  
  584. {Minor changes from TESample--TEStyleNew instead of TENew-makes certain fields in
  585. the edit record (lineHeight, txFont, and txFace) have value of -1 and alloctes new
  586. tables to hold style information}
  587.  
  588. VAR
  589.     good, ignore        : BOOLEAN;
  590.     storage                : Ptr;
  591.     window                : WindowPtr;
  592.     destRect, viewRect    : Rect;
  593.  
  594.  
  595. BEGIN { DoNew }
  596.     storage := NewPtr( SIZEOF( DocumentRecord ) );
  597.     IF storage <> NIL THEN BEGIN
  598.         window := GetNewWindow( rDocWindow, storage, WindowPtr( -1 ) );
  599.         IF window <> NIL THEN BEGIN
  600.             gNumDocuments := gNumDocuments + 1;
  601.             good := FALSE;
  602.             SetPort( window );
  603.             WITH window^, DocumentPeek( window )^ DO BEGIN
  604.                 GetTERect( window, viewRect );
  605.                 destRect := viewRect;
  606.                 destRect.right := destRect.left + kMaxDocWidth;
  607.                 docTE := TEStyleNew( destRect, viewRect );
  608.                 { Use TEStyleNew instead of TENew to initialize TERec correctly }
  609.                 IF docTE <> NIL THEN BEGIN
  610.                     good := TRUE;                {if TENew succeeded, we have a good document}
  611.                     TEAutoView(TRUE, docTE);
  612.                     docClick := docTE^^.clickLoop;
  613.                     docTE^^.clickLoop := @AsmClickLoop;
  614.                 END;
  615.                 IF good THEN BEGIN
  616.                     docVScroll := GetNewControl( rVScroll, window );
  617.                     good := ( docVScroll <> NIL );
  618.                 END; { if }
  619.                 IF good THEN BEGIN
  620.                     docHScroll := GetNewControl( rHScroll, window );
  621.                     good := ( docHScroll <> NIL );
  622.                 END; { if }
  623.                 IF good THEN BEGIN
  624.                     AdjustScrollValues( window, FALSE );
  625.                     ShowWindow( window ); { if the document is good, make the window visible }
  626.                 END ELSE BEGIN
  627.                     ignore := DoCloseWindow( window ); { otherwise regret we ever created it... }
  628.                     AlertUser( eNoWindow ); { and tell user }
  629.                 END { if }
  630.             END; { with }
  631.         END ELSE
  632.             DisposePtr( storage ); { get rid of the storage if it is never used }
  633.     END; { if }
  634. END; { DoNew }
  635.  
  636.  
  637.  
  638. {$S Main}
  639. PROCEDURE BigBadError( error : INTEGER );
  640. BEGIN
  641.     AlertUser( error );
  642.     ExitToShell;
  643. END;
  644.  
  645.  
  646.  
  647. {$S Initialize}
  648. PROCEDURE Initialize;
  649.  
  650. {    Set up the whole world, including global variables, Toolbox managers,
  651.      menus, and a single blank document.}
  652.     
  653. {If an error is detected, instead of merely doing an ExitToShell,
  654.  which leaves the user without much to go on, we call AlertUser, which puts
  655.  up a simple alert that just says an error occurred and then calls ExitToShell.
  656.  Since there is no other cleanup needed at this point if an error is detected,
  657.  this form of error- handling is acceptable. If more sophisticated error recovery
  658.  is needed, an exception mechanism, such as is provided by Signals, can be used.}
  659.  
  660.  
  661. VAR
  662.     menuBar                : Handle;
  663.     total, contig        : LongInt;
  664.     ignoreResult        : BOOLEAN;
  665.     event                : EventRecord;
  666.     count, ignoreError    : INTEGER;
  667.     
  668.     PROCEDURE BigBadError( error : INTEGER );
  669.     BEGIN
  670.         AlertUser( error );
  671.         ExitToShell;
  672.     END;
  673.  
  674. BEGIN { Initialize }
  675.     gInBackground := FALSE;
  676.  
  677.     InitGraf(@qd.thePort);
  678.     InitFonts;
  679.     InitWindows;
  680.     InitMenus;
  681.     TEInit;
  682.     InitDialogs(NIL);
  683.     InitCursor;
  684.  
  685.     FOR count := 1 TO 3 DO
  686.         ignoreResult := EventAvail(everyEvent, event);
  687.     ignoreError := SysEnvirons(kSysEnvironsVersion, gMac);
  688.     IF gMac.machineType < 0 THEN BigBadError(eWrongMachine);
  689.     gHasWaitNextEvent := TrapAvailable(_WaitNextEvent, ToolTrap);
  690.  
  691.     
  692.     IF ORD( GetApplLimit ) - ORD( ApplicationZone ) < kMinHeap THEN
  693.         BigBadError( eSmallSize );
  694.     PurgeSpace( total, contig );
  695.     IF total < kMinSpace THEN
  696.         IF UnloadScrap <> noErr THEN
  697.             BigBadError( eNoMemory )
  698.         ELSE BEGIN
  699.             PurgeSpace( total, contig );
  700.             IF total < kMinSpace THEN
  701.                 BigBadError( eNoMemory );
  702.         END; { if }
  703.  
  704.     menuBar := GetNewMBar( rMenuBar ); { read menus into menu bar }
  705.     IF menuBar = NIL THEN
  706.         BigBadError( eNoMemory );
  707.     SetMenuBar( menuBar ); { install menus }
  708.     DisposeHandle( menuBar );
  709.     AppendResMenu( GetMenuHandle( mApple ), 'DRVR' );    { add DA names to Apple menu }
  710.     AppendResMenu( GetMenuHandle( mFont ),'FONT' ); { add Font names to Font Menu }
  711.     DrawMenuBar;
  712.     gNumDocuments := 0;
  713.  
  714.     { do other initialization here }
  715.     { set up printer stuff-this will allow the default pageSetup parameters to be used, so if
  716.       the used decides to print with out using pageSetup everything will be okay }
  717.       
  718.     gPrinterRecord := THPrint( NewHandle( SizeOF( TPrint ) ) ); {allocate a print record}
  719.     IF gPrinterRecord <> NIL THEN BEGIN {if we're successful then setup the default settings}
  720.         PrOpen; {open the record }
  721.         PrintDefault( gPrinterRecord ); { load in default settings }
  722.         PrClose; { close it up }
  723.     END; { if }
  724.  
  725.     DoNew; { create a single empty document }
  726. END; {Initialize}
  727.  
  728.  
  729.  
  730. {$S Main}    
  731. PROCEDURE PrintText( hTE : TEHandle );
  732.  
  733. {    Prints the edit record. Opens a printer port, calculates the numbers of lines
  734.     per page (it may be different for each page depending on the the text styles) and
  735.     then calls TEUpdate for the page, scroll a page and TEUpdate, etc. }
  736.  
  737. CONST
  738.     Margins                = 20;         { page margins }
  739.     
  740. VAR
  741.     totalLines            : INTEGER;    { number of lines in text }
  742.     rView                : Rect;        { viewRect for TERect }
  743.     oldPort                : grafPtr;    { hold original grafPtr }
  744.     oldView                : Rect;        { hold original viewRect }
  745.     oldDest                : Rect;        { hold original destRect }
  746.     totalHeight            : INTEGER;    { lineHeight for TERec }
  747.     currentLine            : INTEGER;    { what line are we on }
  748.     scrollAmount        : INTEGER;    { how much we scroll by }
  749.     zeroRect            : Rect;        { 0,0,0,0 rect used in clipRect }
  750.     
  751.     thePrinterStatus     : TPrStatus; { printer status }
  752.     openPrintManager    : BOOLEAN;    { flag if print manager can be opened okay }
  753.     abort                : BOOLEAN;    { flag if cmd-period is hit to exit routine }
  754.     viewHeight             : INTEGER;    { temp that has the viewRect height+1 to test conditions }
  755.  
  756.     
  757. BEGIN { PrintText }
  758.     OpenPrintManager := FALSE; {printer not open yet}
  759.     IF gPrinterRecord <> NIL THEN BEGIN { do we have a legitimate record?}
  760.         PrOpen; {open mr. print record if okay}
  761.         IF PrJobDialog( gPrinterRecord ) THEN BEGIN {bring up job dialog}
  762.             GetPort( oldPort ); { save the old stuff to restore later }
  763.             oldView := hTE^^.viewRect; 
  764.             oldDest := hTE^^.destRect;
  765.             gPrinterPort := PrOpenDoc( gPrinterRecord, NIL, NIL );
  766.             OpenPrintManager := ( PrError = noErr );
  767.         END; { if }
  768.     END; { if }
  769.  
  770.     IF OpenPrintManager THEN BEGIN
  771.         SetPort( grafPtr( gPrinterPort ) ); { printer port is now the current port }
  772.         SetRect( zeroRect, 0, 0, 0, 0 );
  773.  
  774.         rView := gPrinterRecord^^.PrInfo.rPage; { get the size of the page rectangle }
  775.         InsetRect( rView, Margins, Margins );  { adjust it for the margins }
  776.         hTE^^.inPort := GrafPtr( gPrinterPort ); { force TE to look at the printer port }
  777.         hTE^^.destRect := rView;
  778.         hTE^^.viewRect := rView; { set new view and dest rects to the TERec }
  779.         TECalText( hTE ); { recalculate our lineStarts array with the new rects }
  780.         totalLines := hTE^^.nLines; { get the number of lines in the newly sized TERec }
  781.         totalHeight := TEGetHeight( totalLines, 0, hTE );
  782.         hTE^^.destRect.bottom := hTE^^.destRect.top + totalHeight; { how tall our destRect is }
  783.         
  784.         abort := FALSE;
  785.         currentLine := 1; { TextEdit sez that TEGetHeight is 1 not 0 based }
  786.         
  787.         WHILE ( NOT ( abort ) AND ( currentLine <= totalLines ) ) DO BEGIN
  788.             PrOpenPage( gPrinterPort, NIL );
  789.             scrollAmount := 0;
  790.             ClipRect( gPrinterRecord^^.PrInfo.rPage ); { Open clipping so text will be drawn }
  791.             
  792.             viewHeight := hTE^^.viewRect.bottom - hTE^^.viewRect.top + 1;
  793.  
  794.             { figure out how many lines there are per page }
  795.             WHILE ((( scrollAmount + TEGetHeight( currentLine, currentLine, hTE ) ) <= viewHeight )
  796.                 AND ( currentLine <= totalLines ) )  DO BEGIN
  797.                 scrollAmount := scrollAmount + TEGetHeight( currentLine, currentLine, hTE );
  798.                 currentLine := currentLine + 1;
  799.             END; { while }
  800.             
  801.             hTE^^.viewRect.bottom := scrollAmount + Margins; { Add margins since top has a margin }
  802.             TEDeactivate( hTE ); { Deactive the edit record so we don't print the cursor or selection range }
  803.             TEUpdate( hTE^^.viewRect, hTE ); { print the page }
  804.             ClipRect( zeroRect ); { Close clipping so that TEScroll doesn't redraw the text }
  805.             TEScroll( 0, -scrollAmount, hTE ); { scroll the page so we can print the next one }
  806.             hTE^^.viewRect.bottom := rView.bottom; { reset bottom to full page }
  807.     
  808.             IF prError = iPrAbort THEN 
  809.                 abort := TRUE;
  810.             PrClosePage( gPrinterPort ); { close everything up }
  811.         END; { while }
  812.  
  813.         PrCloseDoc( gPrinterPort );
  814.         IF ( gPrinterRecord^^.prJob.bJDocLoop = bSpoolLoop ) AND ( PrError = noErr ) THEN
  815.             PrPicFile( gPrinterRecord, NIL, NIL, NIL, thePrinterStatus );
  816.         PrClose;
  817.         SetPort( oldPort );
  818.         hTE^^.inPort := oldPort;
  819.         hTE^^.viewRect := oldView; { restore the old stuff when we are done }
  820.         hTE^^.destRect := oldDest;
  821.         TEUpdate( hTE^^.viewRect, hTE ); { update everything after resetting the port }
  822.     END; { if }
  823. END; { PrintText }
  824.  
  825.  
  826.  
  827. {$S Main}
  828. PROCEDURE Terminate;
  829.  
  830. {    Clean up the application and exit. We close all of the windows so that
  831.     they can update their documents, if any. }
  832.  
  833. VAR
  834.     aWindow    : WindowPtr;
  835.     closed    : BOOLEAN;
  836.  
  837. BEGIN { Terminate }
  838.     closed := TRUE;
  839.     REPEAT
  840.         aWindow := FrontWindow; { get the current front window }
  841.         IF aWindow <> NIL THEN
  842.             closed := DoCloseWindow( aWindow );    { close this window }
  843.     UNTIL ( NOT closed ) | ( aWindow = NIL ); { do all windows }
  844.     IF closed THEN
  845.         ExitToShell; { exit if no cancellation }
  846. END; { Terminate }
  847.  
  848.  
  849.  
  850. {$S Main}
  851. PROCEDURE AdjustMenus;
  852.  
  853. VAR
  854.     window            : WindowPtr;
  855.     menu            : MenuHandle;
  856.     offset            : LONGINT;
  857.     undo            : BOOLEAN;    { flag to enable/disable undo command }
  858.     cutCopyClear    : BOOLEAN;    { flag to enable/disable editing commands }
  859.     paste            : BOOLEAN;    
  860.     selectAll        : BOOLEAN;    
  861.     
  862.     doPrint            : BOOLEAN;    { flag to enable/disable printing item }
  863.     
  864.     te                : TEHandle;    { local te handle }
  865.     mode            : INTEGER;    { current style }
  866.     
  867.  
  868. BEGIN
  869.     window := FrontWindow;
  870.  
  871.     menu := GetMenuHandle( mFile );
  872.     IF gNumDocuments < kMaxOpenDocuments THEN
  873.         EnableItem( menu, iNew ) { New is enabled when we can open more documents }
  874.     ELSE
  875.         DisableItem( menu, iNew );
  876.     
  877.     IF window <> NIL THEN { Close is enabled when there is a window to close }
  878.         EnableItem( menu, iClose )
  879.     ELSE
  880.         DisableItem( menu, iClose );
  881.  
  882.     menu := GetMenuHandle( mEdit );
  883.     undo := FALSE;
  884.     cutCopyClear := FALSE;
  885.     paste := FALSE;
  886.     selectAll := FALSE;
  887.     doPrint := FALSE;
  888.     
  889.     IF IsDAWindow( window ) THEN BEGIN
  890.         undo := TRUE; { all editing is enabled for DA windows }
  891.         cutCopyClear := TRUE;
  892.         paste := TRUE;
  893.         selectAll := TRUE;
  894.     END ELSE IF IsAppWindow( window ) THEN BEGIN
  895.         WITH DocumentPeek( window )^.docTE^^ DO
  896.             IF selStart < selEnd THEN BEGIN
  897.                 cutCopyClear := TRUE;
  898.             END; { if }
  899.                 { Cut, Copy, and Clear is enabled for app. windows with selections }
  900.         IF GetScrap( NIL, 'TEXT', offset ) > 0 THEN
  901.             paste := TRUE; { Paste is enabled for app. windows }
  902.         
  903.         selectAll := TRUE;
  904.         doPrint := TRUE;
  905.             
  906.         mode := doFace;
  907.         menu := GetMenuHandle( mStyle );
  908.         IF TEContinuousStyle( mode, gTxStyle, DocumentPeek( window )^.docTE ) THEN BEGIN
  909.             CheckItem( menu, iPlain, gTxStyle.tsface = [] );
  910.             CheckItem( menu, iBold, bold in gTxStyle.tsFace );
  911.             CheckItem( menu, iItalic, italic in gTxStyle.tsFace );
  912.             CheckItem( menu, iUnderline, underline in gTxStyle.tsFace );
  913.             CheckItem( menu, iOutline, outline in gTxStyle.tsFace );
  914.             CheckItem( menu, iShadow, shadow in gTxStyle.tsFace );
  915.         END ELSE BEGIN
  916.             CheckItem( menu, iPlain, FALSE );
  917.             CheckItem( menu, iBold, FALSE );
  918.             CheckItem( menu, iItalic, FALSE );
  919.             CheckItem( menu, iUnderline, FALSE );
  920.             CheckItem( menu, iOutline, FALSE );
  921.             CheckItem( menu, iShadow, FALSE );
  922.         END; { if }
  923.  
  924.     END; { if }
  925.     menu := GetMenuHandle( mEdit );
  926.     
  927.     IF undo THEN
  928.         EnableItem( menu, iUndo )
  929.     ELSE
  930.         DisableItem( menu, iUndo );
  931.     
  932.     IF cutCopyClear THEN BEGIN
  933.         EnableItem( menu, iCut );
  934.         EnableItem( menu, iCopy );
  935.         EnableItem( menu, iClear );
  936.     END ELSE BEGIN
  937.         DisableItem( menu, iCut );
  938.         DisableItem( menu, iCopy );
  939.         DisableItem( menu, iClear );
  940.     END; { if }
  941.     
  942.     
  943.     IF paste THEN
  944.         EnableItem( menu, iPaste )
  945.     ELSE
  946.         DisableItem( menu, iPaste );
  947.         
  948.     IF selectAll THEN 
  949.         EnableItem( menu, iSelectAll )
  950.     ELSE
  951.         DisableItem( menu, iSelectAll );
  952.  
  953.         
  954.     menu := GetMenuHandle( mFile );
  955.     IF doPrint THEN BEGIN
  956.         EnableItem( menu, iPageSetup );
  957.         EnableItem( menu, iPrint );
  958.     END ELSE BEGIN
  959.         DisableItem( menu, iPageSetup );
  960.         DisableItem( menu, iPrint );
  961.     END; { if }
  962.         
  963.                 
  964. END; { AdjustMenus }
  965.  
  966.  
  967.  
  968. {$S Main}
  969. PROCEDURE DoMenuCommand( menuResult : LONGINT );
  970.  
  971. {    This is called when an item is chosen from the menu bar (after calling
  972.     MenuSelect or MenuKey). It does the right thing for each command. }
  973.  
  974. VAR
  975.     menuID, menuItem        : INTEGER;
  976.     itemHit, daRefNum        : INTEGER;
  977.     daName                    : Str255;
  978.     
  979.     tempStr                    : Str255;
  980.     menu                    : MenuHandle;
  981.     anIntPtr                : ^INTEGER;
  982.     
  983.     ignoreResult, saveErr    : OSErr;
  984.     handledByDA                : BOOLEAN;
  985.     te                        : TEHandle;
  986.     window                    : WindowPtr;
  987.     ignore                    : BOOLEAN;
  988.     aHandle                    : Handle;
  989.     oldSize, newSize        : LONGINT;
  990.     total, contig            : LONGINT;
  991.  
  992.     
  993. BEGIN
  994.     window := FrontWindow;
  995.     menuID := HiWord( menuResult ); { use built-ins (for efficiency)... }
  996.     menuItem := LoWord( menuResult ); { to get menu item number and menu number }
  997.     te := DocumentPeek( window )^.docTE;
  998.                 
  999.     CASE menuID OF
  1000.     
  1001.         mApple:
  1002.             CASE menuItem OF
  1003.                 iAbout:                {bring up alert for About}
  1004.                     itemHit := Alert(rAboutAlert, NIL);
  1005.                 OTHERWISE BEGIN        { all non-About items in this menu are DAs }
  1006.                     GetMenuItemText( GetMenuHandle( mApple ), menuItem, daName );
  1007.                     daRefNum := OpenDeskAcc( daName );
  1008.                 END; { otherwise }
  1009.             END; { case }
  1010.             
  1011.         mFile:
  1012.             CASE menuItem OF
  1013.                 iNew:
  1014.                     DoNew;
  1015.                 iClose:
  1016.                     ignore := DoCloseWindow( window ); { we don't care if cancelled }
  1017.                 iPageSetup: BEGIN
  1018.                     PrOpen;
  1019.                     IF PrError = noErr THEN
  1020.                         ignore := PrStlDialog( gPrinterRecord );
  1021.                     PrClose;
  1022.                 END; { iPageSetup }
  1023.                 iPrint:
  1024.                     PrintText( te );
  1025.                 iQuit:
  1026.                     Terminate;
  1027.             END; { case }
  1028.             
  1029.         mEdit: BEGIN                { call SystemEdit for DA editing & MultiFinder }
  1030.             IF NOT SystemEdit( menuItem -1 ) THEN BEGIN
  1031.                 CASE menuItem OF
  1032.                     
  1033.                     iCut: BEGIN        
  1034.                         IF ZeroScrap = noErr THEN BEGIN
  1035.                             PurgeSpace( total, contig );
  1036.                             IF ( te^^.selEnd - te^^.selStart ) + kTESlop > contig THEN
  1037.                                 AlertUser( eNoSpaceCut )
  1038.                             ELSE BEGIN
  1039.                                 TECut( te );
  1040.                             END; { if }
  1041.                         END; { if }
  1042.                     END; { iCut }
  1043.                     
  1044.                     iCopy: BEGIN
  1045.                         IF ZeroScrap = noErr THEN BEGIN
  1046.                             TECopy( te );
  1047.                         END; { if }
  1048.                     END; { iCopy }
  1049.                     
  1050.                     iPaste: BEGIN
  1051.                         IF TEGetScrapLength + ( te^^.teLength -
  1052.                             ( te^^.selEnd - te^^.selStart ) ) > kMaxTELength THEN
  1053.                             AlertUser( eExceedPaste )
  1054.                         ELSE BEGIN
  1055.                             aHandle := Handle( TEGetText( te ) );
  1056.                             oldSize := GetHandleSize( aHandle );
  1057.                             newSize := oldSize + TEGetScrapLength + kTESlop;
  1058.                             SetHandleSize( aHandle, newSize );
  1059.                             saveErr := MemError;
  1060.                             SetHandleSize( aHandle, oldSize );
  1061.                             IF saveErr <> noErr THEN
  1062.                                 AlertUser( eNoSpacePaste )
  1063.                             ELSE
  1064.                                 TEStylePaste( te );
  1065.                         END; { if }
  1066.                     END; { iPaste }
  1067.                     
  1068.                     iClear:
  1069.                         TEDelete( te );
  1070.                         
  1071.                     iSelectAll:
  1072.                         TESetSelect( 0, te^^.teLength, te );
  1073.                         
  1074.                 END; { case }
  1075.                 if menuItem <> iCopy then
  1076.                     AdjustScrollBars( window, FALSE );
  1077.             END; { if }
  1078.         END; { mEdit }
  1079.         
  1080.         mFont :
  1081.             BEGIN { mFont }
  1082.                 GetMenuItemText( GetMenuHandle( mFont ), menuItem, gFontName );
  1083.                 getFNum( gFontName, gFontID );
  1084.                 gTxStyle.tsFont := gFontID;
  1085.                 TESetStyle( doFont, gTxStyle, true, te );
  1086.                 AdjustScrollBars( window, FALSE );
  1087.             END; { mFont }
  1088.             
  1089.         mFontSize :
  1090.             BEGIN { mFontSize }
  1091.                 CASE menuItem OF
  1092.                     iNine        : gFontSize := 9;
  1093.                     iTen        : gFontSize := 10;
  1094.                     iTwelve        : gFontSize := 12;
  1095.                     iFourteen    : gFontSize := 14;
  1096.                     iEighteen    : gFontSize := 18;
  1097.                     iTwoFour    : gFontSize := 24;
  1098.                 END; { case }
  1099.                 gTxStyle.tsSize := gFontSize;
  1100.                 TESetStyle( doSize, gTxStyle, TRUE, te );
  1101.                 AdjustScrollBars( window, FALSE );
  1102.             END; { mFontSize }
  1103.             
  1104.         mStyle :
  1105.             BEGIN { mStyle }
  1106.                 WITH gTxStyle DO BEGIN
  1107.                     CASE menuItem OF
  1108.                         iPlain         : 
  1109.                             BEGIN
  1110.                                 anIntPtr := @gTxStyle.tsFace; { as per Tech Note #131 }
  1111.                                 anIntPtr^ := 0;
  1112.                                 tsFace := [];
  1113.                             END;
  1114.                         iBold         : tsFace := [bold];
  1115.                         iItalic     : tsFace := [italic];
  1116.                         iUnderline     : tsFace := [underline];
  1117.                         iOutline     : tsFace := [outline];
  1118.                         iShadow     : tsFace := [shadow];
  1119.                     END; { case }                
  1120.                     
  1121.                     IF menuItem <> 1 THEN
  1122.                         TESetStyle( doFace + doToggle, gTxStyle, TRUE, te )
  1123.                          { if we don't select plain then use doToggle }
  1124.                     ELSE
  1125.                         TESetStyle( doFace, gTxStyle, TRUE, te );
  1126.                         { TESetStyle has problems with plain and doToggle-has no effect!
  1127.                           so we need to special case it. }
  1128.                     AdjustScrollBars( window, FALSE );
  1129.                 END; { with }
  1130.             END; { mStyle }
  1131.                         
  1132.     END; { case }
  1133.     HiliteMenu( 0 ); { unhighlight what MenuSelect (or MenuKey) hilited }
  1134. END; { DoMenuCommand }
  1135.  
  1136.  
  1137.  
  1138. {$S Main}
  1139. PROCEDURE DrawWindow( window : WindowPtr );
  1140.  
  1141. {    Draw the contents of an application window. }
  1142.  
  1143. BEGIN { DrawWindow }
  1144.     SetPort( window );
  1145.     WITH window^ DO BEGIN
  1146.         EraseRect( portRect ); { as per TextEdit chapter of Inside Macintosh }
  1147.         DrawControls( window ); { this ordering makes for a better appearance }
  1148.         DrawGrowIcon( window );
  1149.         TEUpdate( portRect, DocumentPeek( window )^.docTE );
  1150.     END; { with }
  1151. END; { DrawWindow }
  1152.  
  1153.  
  1154.  
  1155. {$S Main}
  1156. FUNCTION GetSleep : LONGINT;
  1157.  
  1158. {    Calculate a sleep value for WaitNextEvent. This takes into account the things
  1159.      that DoIdle does with idle time. }
  1160.  
  1161. VAR
  1162.     sleep    : LONGINT;
  1163.     window    : WindowPtr;
  1164.  
  1165. BEGIN { GetSleep }
  1166.     sleep := MAXLONGINT; { default value for sleep }
  1167.     IF NOT gInBackground THEN BEGIN { if we are in front... }
  1168.         window := FrontWindow; { and the front window is ours... }
  1169.         IF IsAppWindow( window ) THEN BEGIN
  1170.             WITH DocumentPeek( window )^.docTE^^ DO
  1171.                 IF selStart = selEnd THEN { and the selection is an insertion point... }
  1172.                     sleep := GetCaretTime; { we need to blink the insertion point }
  1173.         END; { if }
  1174.     END; { if }
  1175.     GetSleep := sleep;
  1176. END; { GetSleep }
  1177.  
  1178.  
  1179.  
  1180. {$S Main}
  1181. PROCEDURE CommonAction( control : ControlHandle; VAR amount : INTEGER );
  1182.  
  1183. {    Common algorithm for setting the new value of a control. It returns the actual amount
  1184.     the value of the control changed. Note the pinning is done for the sake of returning
  1185.     the amount the control value changed. }
  1186.  
  1187. VAR
  1188.     value, max    : INTEGER;
  1189.     window        : WindowPtr;
  1190.  
  1191. BEGIN { CommonAction }
  1192.     value := GetControlValue( control ); { get current value }
  1193.     max := GetControlMaximum( control ); { and max value }
  1194.     amount := value - amount;
  1195.     IF amount < 0 THEN
  1196.         amount := 0
  1197.     ELSE IF amount > max THEN
  1198.         amount := max;
  1199.     SetControlValue( control, amount );
  1200.     amount := value - amount; { calculate true change }
  1201. END; { CommonAction }
  1202.  
  1203.  
  1204.  
  1205. {$S Main}
  1206. PROCEDURE VActionProc( control : ControlHandle; part : INTEGER );
  1207.  
  1208. {    Determines how much to change the value of the vertical scrollbar by and how
  1209.     much to scroll the TE record. }
  1210.  
  1211. VAR
  1212.     amount    : INTEGER;
  1213.     window    : WindowPtr;
  1214.  
  1215. BEGIN { VActionProc }
  1216.     IF part <> 0 THEN BEGIN
  1217.         window := control^^.contrlOwner;
  1218.         WITH DocumentPeek( window )^, DocumentPeek( window )^.docTE^^ DO BEGIN
  1219.             CASE part OF
  1220.                 inUpButton, inDownButton :
  1221.                     amount := 24;
  1222.                 inPageUp, inPageDown :
  1223.                     amount := viewRect.bottom - viewRect.top; { one page }
  1224.             END; { case }
  1225.             IF ( part = inDownButton ) | ( part = inPageDown ) THEN
  1226.                 amount := -amount; { reverse direction }
  1227.             CommonAction( control, amount );
  1228.             IF amount <> 0 THEN
  1229.                 TEScroll( 0, amount, docTE );
  1230.         END; { with }
  1231.     END; { if }
  1232. END; { VActionProc }
  1233.  
  1234.  
  1235.  
  1236. {$S Main}
  1237. PROCEDURE HActionProc( control : ControlHandle; part : INTEGER );
  1238.  
  1239. {    Determines how much to change the value of the horizontal scrollbar by and how
  1240.     much to scroll the TE record. }
  1241.  
  1242. VAR
  1243.     amount    : INTEGER;
  1244.     window    : WindowPtr;
  1245.  
  1246. BEGIN { HActionProc }
  1247.     IF part <> 0 THEN BEGIN
  1248.         window := control^^.contrlOwner;
  1249.         WITH DocumentPeek( window )^, DocumentPeek( window )^.docTE^^ DO BEGIN
  1250.             CASE part OF
  1251.                 inUpButton, inDownButton :
  1252.                     amount := kButtonScroll; { a few pixels }
  1253.                 inPageUp, inPageDown :
  1254.                     amount := viewRect.right - viewRect.left; { a page }
  1255.             END; { case }
  1256.             IF ( part = inDownButton ) | ( part = inPageDown ) THEN
  1257.                 amount := -amount; { reverse direction }
  1258.             CommonAction( control, amount );
  1259.             IF amount <> 0 THEN
  1260.                 TEScroll( amount, 0, docTE );
  1261.         END; { with }
  1262.     END; { if }
  1263. END; { HActionProc }
  1264.  
  1265.  
  1266.  
  1267. {$S Main}
  1268. PROCEDURE DoIdle;
  1269.  
  1270. {    This is called whenever we get an null event or a mouse-moved event.
  1271.      It takes care of necessary periodic actions. For this program, it calls TEIdle. }
  1272.  
  1273. VAR
  1274.     window    : WindowPtr;
  1275.  
  1276. BEGIN { DoIdle }
  1277.     window := FrontWindow;
  1278.     IF IsAppWindow( window ) THEN
  1279.         TEIdle( DocumentPeek( window )^.docTE );
  1280. END; { DoIdle }
  1281.  
  1282.  
  1283.  
  1284. {$S Main}
  1285. PROCEDURE DoKeyDown( event : EventRecord );
  1286.  
  1287. {    This is called for any keyDown or autoKey events, except when the
  1288.     Command key is held down. It looks at the frontmost window to decide what
  1289.      to do with the key typed. }
  1290.  
  1291. VAR
  1292.     window    : WindowPtr;
  1293.     key        : CHAR;
  1294.     te        : TEHandle;
  1295.  
  1296. BEGIN
  1297.     window := FrontWindow;
  1298.     IF IsAppWindow( window ) THEN BEGIN
  1299.         te := DocumentPeek( window)^.docTE;
  1300.         key := CHR( BAnd( event.message, charCodeMask ) );
  1301.         IF ( key = CHR(kDelChar ) ) |     { don't count deletes }
  1302.             ( te^^.teLength - ( te^^.selEnd - te^^.selStart )
  1303.                             + 1 < kMaxTELength ) THEN BEGIN    { but check haven't gone past }
  1304.             TEKey( key, te );
  1305.             AdjustScrollbars( window, FALSE );
  1306.         END ELSE
  1307.             AlertUser( eExceedChar );
  1308.     END; { if }
  1309. END; { DoKeyDown }
  1310.  
  1311.  
  1312.  
  1313. {$S Main}
  1314. PROCEDURE DoContentClick( window : WindowPtr; event : EventRecord );
  1315.  
  1316. {    Called when a mouseDown occurs in the content of a window. }
  1317.  
  1318. VAR
  1319.     mouse        : Point;
  1320.     control        : ControlHandle;
  1321.     part, value    : INTEGER;
  1322.     shiftDown    : BOOLEAN;
  1323.     teRect        : Rect;
  1324.     upp            : ControlActionUPP;
  1325.     
  1326. BEGIN { DoContentClick }
  1327.     IF IsAppWindow( window ) THEN BEGIN
  1328.         SetPort( window );
  1329.         mouse := event.where; { get the click position }
  1330.         GlobalToLocal( mouse ); { convert to local coordinates }
  1331.         
  1332.         GetTERect( window, teRect );
  1333.         IF PtInRect( mouse, teRect ) THEN BEGIN
  1334.             shiftDown := BAnd( event.modifiers, shiftKey ) <> 0; { extend if Shift is down }
  1335.             TEClick( mouse, shiftDown, DocumentPeek( window )^.docTE );
  1336.         END ELSE BEGIN
  1337.             part := FindControl( mouse, window, control );
  1338.             WITH DocumentPeek( window )^ DO
  1339.                 CASE part OF
  1340.                     0:;     { do nothing for viewRect case }
  1341.                     inThumb: BEGIN
  1342.                         value := GetControlValue( control );
  1343.                         part := TrackControl( control, mouse, NIL );
  1344.                         IF part <> 0 THEN BEGIN
  1345.                             value := value - GetControlValue( control );
  1346.                             IF value <> 0 THEN
  1347.                                 IF control = docVScroll THEN
  1348.                                     TEScroll( 0, value, docTE )
  1349.                                 ELSE
  1350.                                     TEScroll( value, 0, docTE );
  1351.                         END; { if }
  1352.                     END; { inThumb }
  1353.                     OTHERWISE    { must be page or button }
  1354.                         BEGIN
  1355.                         IF control = docVScroll THEN
  1356.                             upp := NewControlActionProc(@VActionProc)
  1357.                         ELSE
  1358.                             upp := NewControlActionProc(@HActionProc);
  1359.                         value := TrackControl(control, mouse, upp);
  1360.                         DisposeRoutineDescriptor(upp);
  1361.                         END;
  1362.                 END; { case }
  1363.         END; { if }
  1364.     END; { if }
  1365. END; { DoContentClick }
  1366.  
  1367.  
  1368.  
  1369. {$S Main}
  1370. PROCEDURE ResizeWindow( window : WindowPtr );
  1371.  
  1372. {    Called when the window has been resized to fix up the controls and content }
  1373.  
  1374. BEGIN { ResizeWindow }
  1375.     WITH window^ DO BEGIN
  1376.         AdjustScrollbars( window, TRUE );
  1377.         AdjustTE( window );
  1378.         InvalRect( portRect );
  1379.     END;
  1380. END; { ResizeWindow }
  1381.  
  1382.  
  1383.  
  1384. {$S Main}
  1385. PROCEDURE GetLocalUpdateRgn( window : WindowPtr; localRgn : RgnHandle );
  1386.  
  1387. {    Returns the update region in local coordinates }
  1388.  
  1389. BEGIN { GetLocalUpdateRgn }
  1390.     CopyRgn( WindowPeek( window )^.updateRgn, localRgn ); { save old update region }
  1391.     WITH window^.portBits.bounds DO
  1392.         OffsetRgn( localRgn, left, top ); { convert to local coords }
  1393. END; { GetLocalUpdateRgn }
  1394.  
  1395.  
  1396.  
  1397. {$S Main}
  1398. PROCEDURE DoGrowWindow( window : WindowPtr; event : EventRecord );
  1399.  
  1400. {    Called when a mouseDown occurs in the grow box of an active window. In
  1401.      order to eliminate any 'flicker', we want to invalidate only what is
  1402.      necessary. Since ResizeWindow invalidates the whole portRect, we save
  1403.      the old TE viewRect, intersect it with the new TE viewRect, and
  1404.      remove the result from the update region. However, we must make sure
  1405.      that any old update region that might have been around gets put back. }
  1406.  
  1407. VAR
  1408.     growResult        : LONGINT;
  1409.     tempRect        : Rect;
  1410.     tempRgn            : RgnHandle;
  1411.     ignoreResult    : BOOLEAN;
  1412.  
  1413. BEGIN { DoGrowWindow }
  1414.     WITH qd.screenBits.bounds DO
  1415.         SetRect( tempRect, kMinDocDim, kMinDocDim, right, bottom ); { set up limiting values }
  1416.     growResult := GrowWindow( window, event.where, tempRect );
  1417.     IF growResult <> 0 THEN  { see if changed size }
  1418.         WITH DocumentPeek( window )^, window^ DO BEGIN
  1419.             tempRect := docTE^^.viewRect; { save old text box }
  1420.             tempRgn := NewRgn;
  1421.             GetLocalUpdateRgn( window, tempRgn ); { get localized update region }
  1422.             SizeWindow( window, LoWord( growResult ), HiWord( growResult ), TRUE );
  1423.             ResizeWindow( window );
  1424.             ignoreResult := SectRect( tempRect, docTE^^.viewRect, tempRect ); { find what stayed same }
  1425.             ValidRect( tempRect ); { take it out of update }
  1426.             InvalRgn( tempRgn ); { put back any prior update }
  1427.             DisposeRgn( tempRgn );
  1428.         END; { with }
  1429. END; { DoGrowWindow }
  1430.  
  1431.  
  1432.  
  1433. {$S Main}
  1434. PROCEDURE DoZoomWindow( window : WindowPtr; part : INTEGER );
  1435.  
  1436. {    Called when a mouseClick occurs in the zoom box of an active window.
  1437.     Everything has to get re-drawn here, so we don't mind that
  1438.     ResizeWindow invalidates the whole portRect. }
  1439.  
  1440. BEGIN { DoZoomWindow }
  1441.     WITH window^ DO BEGIN
  1442.         EraseRect( portRect );
  1443.         ZoomWindow( window, part, ( window = FrontWindow ) );
  1444.         ResizeWindow( window );
  1445.     END; { with }
  1446. END; { DoZoomWindow }
  1447.  
  1448.  
  1449.  
  1450. {$S Main}
  1451. PROCEDURE DoUpdate( window : WindowPtr );
  1452.  
  1453. {    This is called when an update event is received for a window.
  1454.      It calls DrawWindow to draw the contents of an application window,
  1455.      but only if the visRgn is non-empty; for efficiency reasons,
  1456.      not because it is required. }
  1457.  
  1458. BEGIN { DoUpdate }
  1459.     IF IsAppWindow( window ) THEN BEGIN
  1460.         BeginUpdate( window ); { this sets up the visRgn }
  1461.         IF NOT EmptyRgn( window^.visRgn ) THEN    { draw if updating needs to be done }
  1462.             DrawWindow( window );
  1463.         EndUpdate( window );
  1464.     END; { if }
  1465. END; { DoUpdate }
  1466.  
  1467.  
  1468.  
  1469. {$S Main}
  1470. PROCEDURE DoActivate( window : WindowPtr; becomingActive : BOOLEAN );
  1471.  
  1472. {    This is called when a window is activated or deactivated. }
  1473.  
  1474. VAR
  1475.     tempRgn, clipRgn    : RgnHandle;
  1476.     growRect            : Rect;
  1477.  
  1478. BEGIN { DoActivate }
  1479.     IF IsAppWindow( window ) THEN
  1480.         WITH DocumentPeek( window )^ DO
  1481.             IF becomingActive THEN BEGIN
  1482.                 { since we don’t want TEActivate to draw a selection in an area where
  1483.                   we’re going to erase and redraw, we’ll clip out the update region
  1484.                   before calling it. }
  1485.                 tempRgn := NewRgn;
  1486.                 clipRgn := NewRgn;
  1487.                 GetLocalUpdateRgn( window, tempRgn ); { get localized update region }
  1488.                 GetClip( clipRgn );
  1489.                 DiffRgn( clipRgn, tempRgn, tempRgn ); { subtract updateRgn from clipRgn }
  1490.                 SetClip( tempRgn );
  1491.                 TEActivate( docTE ); { let TE do its thing }
  1492.                 SetClip( clipRgn ); { restore the full-blown clipRgn }
  1493.                 DisposeRgn( tempRgn );
  1494.                 DisposeRgn( clipRgn );
  1495.  
  1496.                 {the controls need to be redrawn on activation:}
  1497.                 docVScroll^^.contrlVis := kControlVisible;
  1498.                 docHScroll^^.contrlVis := kControlVisible;
  1499.                 InvalRect( docVScroll^^.contrlRect );
  1500.                 InvalRect( docHScroll^^.contrlRect );
  1501.                 { the growbox needs to be redrawn on activation: }
  1502.                 growRect := window^.portRect;
  1503.                 WITH growRect DO BEGIN
  1504.                     top := bottom - kScrollbarAdjust; { adjust for the scrollbars }
  1505.                     left := right - kScrollbarAdjust;
  1506.                 END; { with }
  1507.                 InvalRect( growRect );
  1508.             END ELSE BEGIN
  1509.                 TEDeactivate( docTE );
  1510.                 { the controls should be hidden immediately on deactivation: }
  1511.                 HideControl( docVScroll );
  1512.                 HideControl( docHScroll );
  1513.                 { the growbox should be changed immediately on deactivation: }
  1514.                 DrawGrowIcon( window );
  1515.             END; { if }
  1516. END; { DoActivate }
  1517.  
  1518.  
  1519.  
  1520. {$S Main}
  1521. PROCEDURE GetGlobalMouse(VAR mouse: Point);
  1522.  
  1523. {Get the global coordinates of the mouse. When you call OSEventAvail
  1524.  it will return either a pending event or a null event. In either case,
  1525.  the where field of the event record will contain the current position
  1526.  of the mouse in global coordinates and the modifiers field will reflect
  1527.  the current state of the modifiers. Another way to get the global
  1528.  coordinates is to call GetMouse and LocalToGlobal, but that requires
  1529.  being sure that thePort is set to a valid port.}
  1530.  
  1531. VAR
  1532.     event    : EventRecord;
  1533.     
  1534. BEGIN
  1535.     IF OSEventAvail(kNoEvents, event) THEN;    {we aren't interested in any events}
  1536.     mouse := event.where;                    {just the mouse position}
  1537. END;
  1538.  
  1539.  
  1540.  
  1541. {$S Main}
  1542. PROCEDURE AdjustCursor( mouse : Point; region : RgnHandle );
  1543.  
  1544. { Change the cursor's shape, depending on its position. This also calculates a region
  1545.  that includes the cursor for WaitNextEvent. }
  1546.  
  1547. VAR
  1548.     window        : WindowPtr;
  1549.     arrowRgn    : RgnHandle;
  1550.     iBeamRgn    : RgnHandle;
  1551.     iBeamRect    : Rect;
  1552.  
  1553. BEGIN { AdjustCursor }
  1554.     window := FrontWindow; { we only adjust the cursor when we are in front }
  1555.     IF ( NOT gInBackground ) AND ( NOT IsDAWindow( window ) ) THEN BEGIN
  1556.         { calculate regions for different cursor shapes}
  1557.         arrowRgn := NewRgn;
  1558.         iBeamRgn := NewRgn;
  1559.  
  1560.         { start with a big, big rectangular region }
  1561.         SetRectRgn( arrowRgn, kExtremeNeg, kExtremeNeg, kExtremePos, kExtremePos );
  1562.  
  1563.         { calculate iBeamRgn }
  1564.         IF IsAppWindow( window ) THEN BEGIN
  1565.             iBeamRect := DocumentPeek( window )^.docTE^^.viewRect;
  1566.             SetPort( window ); { make a global version of the viewRect }
  1567.             WITH iBeamRect DO BEGIN
  1568.                 LocalToGlobal( topLeft );
  1569.                 LocalToGlobal( botRight );
  1570.             END; { with }
  1571.             RectRgn( iBeamRgn, iBeamRect );
  1572.             WITH window^.portBits.bounds DO
  1573.                 SetOrigin( -left, -top );
  1574.             SectRgn( iBeamRgn, window^.visRgn, iBeamRgn );
  1575.             SetOrigin( 0, 0 );
  1576.         END; { if }
  1577.  
  1578.         { subtract other regions from arrowRgn }
  1579.         DiffRgn( arrowRgn, iBeamRgn, arrowRgn );
  1580.         
  1581.         {change the cursor and the region parameter}
  1582.         IF PtInRgn( mouse, iBeamRgn ) THEN BEGIN
  1583.             SetCursor( GetCursor( iBeamCursor )^^ );
  1584.             CopyRgn( iBeamRgn, region );
  1585.         END ELSE BEGIN
  1586.             SetCursor( qd.arrow );
  1587.             CopyRgn( arrowRgn, region );
  1588.         END; { if }
  1589.  
  1590.         { get rid of our local regions }
  1591.         DisposeRgn( arrowRgn );
  1592.         DisposeRgn( iBeamRgn );
  1593.     END; { if }
  1594. END; { AdjustCursor }
  1595.  
  1596.  
  1597.  
  1598. {$S Main}
  1599. PROCEDURE DoEvent( event : EventRecord );
  1600.  
  1601. {    Do the right thing for an event. Determine what kind of event it is, and call
  1602.      the appropriate routines. }
  1603.  
  1604. VAR
  1605.     part, err    : INTEGER;
  1606.     window        : WindowPtr;
  1607.     key            : CHAR;
  1608.     ignore        : BOOLEAN;
  1609.     aPoint        : Point;
  1610.  
  1611. BEGIN { DoEvent }
  1612.     CASE event.what OF
  1613.         nullEvent:
  1614.             DoIdle;
  1615.         mouseDown: BEGIN
  1616.             part := FindWindow( event.where, window );
  1617.             CASE part OF
  1618.                 
  1619.                 inMenuBar : BEGIN
  1620.                     AdjustMenus;
  1621.                     DoMenuCommand( MenuSelect( event.where ) );
  1622.                 END; { inMenuBar }
  1623.                 
  1624.                 inSysWindow :
  1625.                     SystemClick( event, window );
  1626.                 
  1627.                 inContent :
  1628.                     IF window <> FrontWindow THEN BEGIN
  1629.                         SelectWindow(window);
  1630.                         {DoEvent(event);}    {use this line for "do first click"}
  1631.                     END ELSE
  1632.                         DoContentClick( window, event );
  1633.                 
  1634.                 inDrag :
  1635.                     DragWindow( window, event.where, qd.screenBits.bounds );
  1636.                 
  1637.                 inGrow:
  1638.                     DoGrowWindow( window, event );
  1639.                 
  1640.                 inGoAway:
  1641.                     IF TrackGoAway( window, event.where ) THEN
  1642.                         ignore := DoCloseWindow( window ); { we don't care if cancelled }
  1643.                 
  1644.                 inZoomIn, inZoomOut:
  1645.                 
  1646.                     IF TrackBox(window, event.where, part) THEN
  1647.                         DoZoomWindow( window, part );
  1648.             END; { case }
  1649.         END; { mouseDown }
  1650.         
  1651.         keyDown, autoKey : BEGIN
  1652.             key := CHR( BAnd( event.message, charCodeMask ) );
  1653.             IF BAnd( event.modifiers, cmdKey ) <> 0 THEN BEGIN { Command key down }
  1654.                 IF event.what = keyDown THEN BEGIN
  1655.                     AdjustMenus; { enable/disable/check menu items properly }
  1656.                     DoMenuCommand( MenuKey( key ) );
  1657.                 END; { if }
  1658.             END ELSE
  1659.                 DoKeyDown( event );
  1660.         END; { keyDown } { call DoActivate with the window and... }
  1661.         
  1662.         activateEvt: { TRUE for activate, FALSE for deactivate }
  1663.             DoActivate( WindowPtr( event.message ), BAND( event.modifiers, activeFlag ) <> 0 );
  1664.         
  1665.         updateEvt: { call DoUpdate with the window to update }
  1666.             DoUpdate( WindowPtr( event.message ) );
  1667.             
  1668.         diskEvt:
  1669.             IF HiWord(event.message) <> noErr THEN BEGIN
  1670.                 SetPt(aPoint, kDILeft, kDITop);
  1671.                 err := DIBadMount(aPoint, event.message);
  1672.             END;
  1673.  
  1674.         kOSEvent:
  1675.             CASE BAnd(BRotL( event.message, 8 ), $FF ) OF    { high byte of message }
  1676.                 kMouseMovedMessage:
  1677.                     DoIdle; { mouse moved is also an idle event }
  1678.                 
  1679.                 kSuspendResumeMessage: BEGIN
  1680.                     gInBackground := BAnd( event.message, kResumeMask ) = 0;
  1681.                     DoActivate( FrontWindow, NOT gInBackground );
  1682.                 END; { kSuspendResumeMessage }
  1683.         END; 
  1684.     END; { case }
  1685. END; { DoEvent }
  1686.  
  1687.  
  1688.  
  1689. {$S Main}
  1690. PROCEDURE EventLoop;
  1691.  
  1692. {    Get events forever, and handle them by calling DoEvent.
  1693.      Also call AdjustCursor each time through the loop. }
  1694.  
  1695. VAR
  1696.     cursorRgn        : RgnHandle;
  1697.     gotEvent        : BOOLEAN;
  1698.     event            : EventRecord;
  1699.     mouse            : Point;
  1700.  
  1701. BEGIN { EventLoop }
  1702.     cursorRgn := NewRgn; { we'll pass an empty region to WNE the first time thru }
  1703.     REPEAT
  1704.         IF gHasWaitNextEvent THEN BEGIN
  1705.             GetGlobalMouse(mouse);        {since we might go to sleep}
  1706.             AdjustCursor(mouse, cursorRgn);
  1707.             gotEvent := WaitNextEvent(everyEvent, event, GetSleep, cursorRgn)
  1708.         END ELSE BEGIN
  1709.             SystemTask;
  1710.             gotEvent := GetNextEvent(everyEvent, event);
  1711.         END; { if }
  1712.         IF gotEvent THEN BEGIN
  1713.             AdjustCursor(event.where, cursorRgn);
  1714.             DoEvent(event);
  1715.             END
  1716.         ELSE
  1717.             DoIdle;
  1718.     UNTIL FALSE; { loop forever }
  1719. END; { EventLoop }
  1720.  
  1721.  
  1722.  
  1723. PROCEDURE _DataInit; EXTERNAL;
  1724.  
  1725. {    This routine is automatically linked in by the MPW Linker. This external
  1726.     reference to it is done so that we can unload its segment, %A5Init. }
  1727.  
  1728.  
  1729. {$S Main}
  1730. BEGIN { main program }
  1731.     UnloadSeg( @_DataInit );    { note that _DataInit must not be in Main! }
  1732.     MaxApplZone;                { expand the heap so code segments load at the top }
  1733.     Initialize;                    { initialize the program }
  1734.     UnloadSeg( @Initialize );    { note that Initialize must not be in Main! }
  1735.      EventLoop;                    { call the main event loop }
  1736. END. { main program }
  1737.